package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description:
 * @author 李杰
 * @date 2019/1/11 10:02
 * @version
 * @since
 * Modified by:
 */
@Getter
@Setter
public class CommunicatePlanRemindPushSet extends AbstractPushSet {

    /**{
        "role": {				推送的角色
            "enable": 1,
            "value": ["10",
            "30"]
        },
        "dep": {				推送的部门
            "enable": 0,
            "value": ["2",
            "5"]
        },
        "user": {				推送的指定人
            "enable": 0,
            "value": []
        }
    }
    */
    private String pushObject;


    /**{
        "time": 32400,			//每天几点推送 00：00 + 32400秒 即8:00PM推送
        "timeInterval": 0,		不使用
        "timeIntervalType": 1,	//每天推送
        "type": 3				//循环推送
    }
     */
    private String pushTime;

    private static final Integer HALF_HOUR = 30*60;
    private static final Integer ONE_HOUR = 3600;
    private static final Integer THREE_HOUR = 3*3600;
    private static final Integer ONE_DAY = 24*3600;

    private static final Integer HALF_HOUR_TYPE = 1;
    private static final Integer ONE_HOUR_TYPE = 2;
    private static final Integer THREE_HOUR_TYPE = 3;
    private static final Integer ONE_DAY_TYPE = 4;

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime, String attrEntity) {
        setPushObject(pushObject);
        setPushTime(pushTime);

        PushSetEntity pushSetEntity = new PushSetEntity();

        pushSetEntity.setAddTime(addTime);
        pushSetEntity.setAttrEntity(attrEntity);
        pushSetEntity.setCorpid(corpid);
        pushSetEntity.setDel(del);
        pushSetEntity.setEnable(enable);
        pushSetEntity.setId(id);
        pushSetEntity.setPushObject(pushObject);
        pushSetEntity.setPushTime(pushTime);
        pushSetEntity.setRefEntity(refEntity);
        pushSetEntity.setSubType(subType);
        pushSetEntity.setType(type);
        pushSetEntity.setUpdateTime(updateTime);

        return pushSetEntity;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType, PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.COMMUNICATE_PLAN_REMIND.getCode();

        Map<String,Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("type", type);
        param.put("subType", subType);

        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);

        if(pushSetList.isEmpty()){
            PushSetEntity pushSetEntity = new PushSetEntity();
            pushSetEntity.setType(type);
            pushSetEntity.setSubType(subType);
            JSONObject pushObject = new JSONObject();
            /************推送对象设置*************/
            pushObject.put("type", "|" + PushObjectTypeEnum.EXECUTOR.getCode() + "|");

            //user
            JSONObject userObject = new JSONObject();
            userObject.put("enable", 0);
            List<String> userIdList = new ArrayList<>();
            userObject.put("value", userIdList);
            pushObject.put("user", userObject);
            pushSetEntity.setPushObject(pushObject.toJSONString());
            // 推送时间设置
            JSONObject timeObject = new JSONObject();
            //即时推送
            timeObject.put("type", PushTimeTypeEnum.ADVANCE.getCode());
            //默认8:00
            timeObject.put("time", 0);
            //无效时间间隔
            timeObject.put("period", CommunicatePlanRemindPushSet.HALF_HOUR);
            //每天提醒
            timeObject.put("timeIntervalType", 0);
            //循环
            timeObject.put("loop", 0);
            pushSetEntity.setPushTime(timeObject.toJSONString());
            pushSetEntity.setEnable(1);
            pushSetList.add(pushSetEntity);
        }

        return pushSetList.get(0);
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        Integer timeType = PushTimeTypeEnum.ADVANCE.getCode();
        Integer time = -1;
        Integer period = 0;
        Integer timeIntervalType = 0;
        Integer loop = 0;
        Integer pushTimeType = jsonObject.getInteger("pushTimeType");
        if(Objects.equals(pushTimeType, HALF_HOUR_TYPE)){
            period = HALF_HOUR;
        }else if(Objects.equals(pushTimeType, ONE_HOUR_TYPE)){
            period = ONE_HOUR;
        }else if(Objects.equals(pushTimeType, THREE_HOUR_TYPE)){
            period = THREE_HOUR;
        }else if(Objects.equals(pushTimeType, ONE_DAY_TYPE)){
            period = ONE_DAY;
        }
        JSONObject retObject = new JSONObject();
        retObject.put("timeType", timeType);
        retObject.put("time", time);
        retObject.put("period", period);
        retObject.put("timeIntervalType", timeIntervalType);
        retObject.put("loop", loop);
        return retObject;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, String attr,
                                    PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }
}
